from rest_framework.decorators import api_view
from rest_framework.response import Response
from .utils import handle_message

@api_view(['POST'])
def chat(request):
    data = request.data
    session_id = data.get('session_id', 'default_user')  # 可以用 cookie / token 生成唯一ID
    user_input = data.get('message', '').strip()

    if not user_input:
        return Response({"error": "请输入消息内容"}, status=400)

    result = handle_message(session_id, user_input)

    return Response({
        "output": result["output"],
        "options": result.get("options", {}),
        "next_state": result.get("next_state"),
        "is_ending": result.get("is_ending", False)
    })